

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.nn.MultiFieldEmbeddingLookup &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="mindspore.nn.SparseToDense" href="mindspore.nn.SparseToDense.html" />
    <link rel="prev" title="mindspore.nn.EmbeddingLookup" href="mindspore.nn.EmbeddingLookup.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../mindspore.nn.html">mindspore.nn</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id1">基本构成单元</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id2">容器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id3">卷积层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id4">梯度</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id5">循环层</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../mindspore.nn.html#id6">稀疏层</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Embedding.html">mindspore.nn.Embedding</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.EmbeddingLookup.html">mindspore.nn.EmbeddingLookup</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">mindspore.nn.MultiFieldEmbeddingLookup</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.SparseToDense.html">mindspore.nn.SparseToDense</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.SparseTensorDenseMatmul.html">mindspore.nn.SparseTensorDenseMatmul</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id7">非线性激活函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id8">工具</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id9">图像</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id10">归一化层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id11">池化层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id12">量化</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id13">损失函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id14">优化器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#wrapper">Wrapper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id15">数学运算</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id16">评估指标</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id17">动态学习率</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../mindspore.nn.html">mindspore.nn</a> &raquo;</li>
        
      <li>mindspore.nn.MultiFieldEmbeddingLookup</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../_sources/api_python/nn/mindspore.nn.MultiFieldEmbeddingLookup.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-nn-multifieldembeddinglookup">
<h1>mindspore.nn.MultiFieldEmbeddingLookup<a class="headerlink" href="#mindspore-nn-multifieldembeddinglookup" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="mindspore.nn.MultiFieldEmbeddingLookup">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.nn.</code><code class="sig-name descname">MultiFieldEmbeddingLookup</code><span class="sig-paren">(</span><em class="sig-param">vocab_size</em>, <em class="sig-param">embedding_size</em>, <em class="sig-param">field_size</em>, <em class="sig-param">param_init=&quot;normal&quot;</em>, <em class="sig-param">target=&quot;CPU&quot;</em>, <em class="sig-param">slice_mode=&quot;batch_slice&quot;</em>, <em class="sig-param">feature_num_list=None</em>, <em class="sig-param">max_norm=None</em>, <em class="sig-param">sparse=True</em>, <em class="sig-param">operator=&quot;SUM&quot;</em><span class="sig-paren">)</span><a class="reference internal" href="../../_modules/mindspore/nn/layer/embedding.html#MultiFieldEmbeddingLookup"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#mindspore.nn.MultiFieldEmbeddingLookup" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a slice of input tensor based on the specified indices and the field ids. This operation
supports looking up embeddings using multi hot and one hot fields simultaneously.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>When ‘target’ is set to ‘CPU’, this module will use
P.EmbeddingLookup().add_prim_attr(‘primitive_target’, ‘CPU’) which
specified ‘offset = 0’ to lookup table.
When ‘target’ is set to ‘DEVICE’, this module will use P.Gather() which
specified ‘axis = 0’ to lookup table.
The vectors with the same field_ids  will be combined by the <cite>operator</cite>, such as ‘SUM’, ‘MAX’ and
‘MEAN’. Ensure the input_values of the padded id is zero, so that they can be ignored. The final
output will be zeros if the sum of absolute weight of the field is zero. This class only
supports [‘table_row_slice’, ‘batch_slice’ and ‘table_column_slice’]. For the operation ‘MAX’ on
device Ascend, there is a constraint where batch_size * (seq_length + field_size) &lt; 3500.</p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>vocab_size</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#int" title="(in Python v3.8)"><em>int</em></a>) – The size of the dictionary of embeddings.</p></li>
<li><p><strong>embedding_size</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#int" title="(in Python v3.8)"><em>int</em></a>) – The size of each embedding vector.</p></li>
<li><p><strong>field_size</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#int" title="(in Python v3.8)"><em>int</em></a>) – The field size of the final outputs.</p></li>
<li><p><strong>param_init</strong> (<em>Union</em><em>[</em><a class="reference internal" href="../mindspore/mindspore.Tensor.html#mindspore.Tensor" title="mindspore.Tensor"><em>Tensor</em></a><em>, </em><a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a><em>, </em><a class="reference internal" href="../mindspore.common.initializer.html#mindspore.common.initializer.Initializer" title="mindspore.common.initializer.Initializer"><em>Initializer</em></a><em>, </em><a class="reference external" href="https://docs.python.org/library/numbers.html#numbers.Number" title="(in Python v3.8)"><em>numbers.Number</em></a><em>]</em>) – Initializer for the embedding_table.
Refer to class <cite>initializer</cite> for the values of string when a string
is specified. Default: ‘normal’.</p></li>
<li><p><strong>target</strong> (<a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a>) – Specifies the target where the op is executed. The value must in
[‘DEVICE’, ‘CPU’]. Default: ‘CPU’.</p></li>
<li><p><strong>slice_mode</strong> (<a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a>) – The slicing way in semi_auto_parallel/auto_parallel. The value must get through
<a class="reference internal" href="mindspore.nn.EmbeddingLookup.html#mindspore.nn.EmbeddingLookup" title="mindspore.nn.EmbeddingLookup"><code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.nn.EmbeddingLookup</span></code></a>. Default: <code class="xref py py-class docutils literal notranslate"><span class="pre">mindspore.nn.EmbeddingLookup.BATCH_SLICE</span></code>.</p></li>
<li><p><strong>feature_num_list</strong> (<a class="reference external" href="https://docs.python.org/library/stdtypes.html#tuple" title="(in Python v3.8)"><em>tuple</em></a>) – The accompaniment array in field slice mode. This is unused currently. Default: None.</p></li>
<li><p><strong>max_norm</strong> (<em>Union</em><em>[</em><a class="reference external" href="https://docs.python.org/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a><em>, </em><a class="reference external" href="https://docs.python.org/library/constants.html#None" title="(in Python v3.8)"><em>None</em></a><em>]</em>) – A maximum clipping value. The data type must be float16, float32
or None. Default: None</p></li>
<li><p><strong>sparse</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#bool" title="(in Python v3.8)"><em>bool</em></a>) – Using sparse mode. When ‘target’ is set to ‘CPU’, ‘sparse’ has to be true. Default: True.</p></li>
<li><p><strong>operator</strong> (<a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a>) – The pooling method for the features in one field. Support ‘SUM’, ‘MEAN’ and ‘MAX’.
Default: ‘SUM’.</p></li>
</ul>
</dd>
</dl>
<dl class="simple">
<dt>Inputs:</dt><dd><ul class="simple">
<li><p><strong>input_indices</strong> (Tensor) - The shape of tensor is <span class="math notranslate nohighlight">\((batch\_size, seq\_length)\)</span>.
Specifies the indices of elements of the original Tensor. Input_indices must be a 2d tensor in
this interface. Type is Int32, Int64.</p></li>
<li><p><strong>input_values</strong> (Tensor) - The shape of tensor is <span class="math notranslate nohighlight">\((batch\_size, seq\_length)\)</span>.
Specifies the weights of elements of the input_indices. The lookout vector will multiply with
the input_values. Type is Float32.</p></li>
<li><p><strong>field_ids</strong> (Tensor)  - The shape of tensor is <span class="math notranslate nohighlight">\((batch\_size, seq\_length)\)</span>.
Specifies the field id of elements of the input_indices. Type is Int32.</p></li>
</ul>
</dd>
<dt>Outputs:</dt><dd><p>Tensor, the shape of tensor is <span class="math notranslate nohighlight">\((batch\_size, field\_size, embedding\_size)\)</span>. Type is Float32.</p>
</dd>
</dl>
<dl class="field-list simple">
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><ul class="simple">
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If <cite>vocab_size</cite> or <cite>embedding_size</cite> or <cite>field_size</cite> is not an int.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If <cite>sparse</cite> is not a bool or <cite>feature_num_list</cite> is not a tuple.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#ValueError" title="(in Python v3.8)"><strong>ValueError</strong></a> – If <cite>vocab_size</cite> or <cite>embedding_size</cite> or <cite>field_size</cite> is less than 1.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#ValueError" title="(in Python v3.8)"><strong>ValueError</strong></a> – If <cite>target</cite> is neither ‘CPU’ nor ‘DEVICE’.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#ValueError" title="(in Python v3.8)"><strong>ValueError</strong></a> – If <cite>slice_mode</cite> is not one of ‘batch_slice’, ‘field_slice’, ‘table_row_slice’,
    ‘table_column_slice’.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#ValueError" title="(in Python v3.8)"><strong>ValueError</strong></a> – If <cite>sparse</cite> is False and <cite>target</cite> is ‘CPU’.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#ValueError" title="(in Python v3.8)"><strong>ValueError</strong></a> – If <cite>slice_mode</cite> is ‘field_slice’ and <cite>feature_num_list</cite> is None.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#ValueError" title="(in Python v3.8)"><strong>ValueError</strong></a> – If <cite>operator</cite> is not one of ‘SUM’, ‘MAX’, ‘MEAN’.</p></li>
</ul>
</dd>
</dl>
<dl class="simple">
<dt>Supported Platforms:</dt><dd><p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">input_indices</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">input_values</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">field_ids</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]],</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">net</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">MultiFieldEmbeddingLookup</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">field_size</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">operator</span><span class="o">=</span><span class="s1">&#39;SUM&#39;</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><span class="s1">&#39;DEVICE&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">out</span> <span class="o">=</span> <span class="n">net</span><span class="p">(</span><span class="n">input_indices</span><span class="p">,</span> <span class="n">input_values</span><span class="p">,</span> <span class="n">field_ids</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">out</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(2, 2, 2)</span>
</pre></div>
</div>
</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.nn.SparseToDense.html" class="btn btn-neutral float-right" title="mindspore.nn.SparseToDense" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="mindspore.nn.EmbeddingLookup.html" class="btn btn-neutral float-left" title="mindspore.nn.EmbeddingLookup" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>